﻿//12. Extend the program to support also subtraction and multiplication of polynomials.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class MultiplyPolynomials
{
    static void Main()
    {
        int[] firstPolynom = { 3, -5, 6, 0, 7 };
        int[] secondPolynom = { 0, 2, 3 };
        Console.WriteLine("First polynomial: ".PadLeft(20, '-') + PolynomToString(firstPolynom));
        Console.WriteLine("Second polynomial: ".PadLeft(20, '-') + PolynomToString(secondPolynom));
        int[] multiply = MultiplyPoly(firstPolynom, secondPolynom);
        Console.WriteLine("\n" + "Sum: ".PadLeft(20, '-') + PolynomToString(multiply));
    }

    static int[] MultiplyPoly(int[] firstPolynom, int[] secondPolynom)
    {
        int[] current = new int[(firstPolynom.Length + secondPolynom.Length) - 1];
        for (int i = 0; i < firstPolynom.Length; i++)
        {
            for (int j = 0; j < secondPolynom.Length; j++)
            {
                current[i + j] = current[i + j] + firstPolynom[i] * secondPolynom[j];
            }
        }
        return current;
    }

    static string PolynomToString(int[] polynomial)
    {
        StringBuilder result = new StringBuilder();

        for (int i = polynomial.Length - 1; i >= 0; i--)
        {
            if (polynomial[i] != 0)
            {
                result.Append(polynomial[i] > 0 ? "+" : "-");
                result.Append(Math.Abs(polynomial[i]));
                if (i != 0)
                {
                    result.Append(i > 1 ? "x^" + i : "x");
                }
            }
        }
        return result.ToString();
    }
}
